<?php
namespace app\api\service\ceping;

// ====================== 核心算法 ======================
class XuexituoyanService {
	//采用李克特5级量表（1=完全不符合，5=完全符合）：
	// ====================== 题库配置 ======================
	public $questionBank = [
		 '任务启动' => [
            ['id' => 1, 'text' => '我经常把学习任务推迟到截止日期前才开始', 'type' => '正向', 'weight' => 1.3],
            ['id' => 2, 'text' => '我能按计划准时开始学习任务', 'type' => '反向', 'weight' => 1.2]
        ],
        '时间感知' => [
            ['id' => 3, 'text' => '我经常低估完成学习任务所需的时间', 'type' => '正向', 'weight' => 1.1],
            ['id' => 4, 'text' => '我能准确预估作业所需时间', 'type' => '反向', 'weight' => 1.0]
        ],
        '分心抵抗' => [
            ['id' => 5, 'text' => '学习时容易被手机消息打断', 'type' => '正向', 'weight' => 1.1],
            ['id' => 6, 'text' => '我能连续专注学习1小时以上', 'type' => '反向', 'weight' => 1.2]
        ],
        '自我调节' => [
            ['id' => 7, 'text' => '即使任务枯燥我也能坚持完成', 'type' => '反向', 'weight' => 1.3],
            ['id' => 8, 'text' => '我经常用娱乐活动逃避学习', 'type' => '正向', 'weight' => 1.2]
        ]
	];
    // 生成测评试卷
    public function generateTest($numPerDimension = 2) {
        $test = [];
        foreach ($this->questionBank  as $dim => $questions) {
            shuffle($questions);
            $test = array_merge($test, array_slice($this->questionBank[$dim], 0, $numPerDimension));
        }
        return $test;
    }

    // 计算得分
    // 计算能力得分
    public function calculateScore($answers) {
        $dimensionScores = [];
        $totalScore = 0;
        $maxPossible = 0;

        foreach ($this->questionBank as $dim => $questions) {
            $dimScore = 0;
            foreach ($questions as $q) {
                if (!isset($answers[$q['id']])) continue;

                // 处理反向计分
                $effectiveScore = ($q['type'] == '反向') ? 
                                 (6 - $answers[$q['id']]) : 
                                 $answers[$q['id']];
                
                $dimScore += $effectiveScore * $q['weight'];
            }
            // 转换为百分制
            $maxDim = 5 * count($questions) * $q['weight'];
            $dimensionScores[$dim] = round(($dimScore / $maxDim) * 100, 1);
            $totalScore += $dimScore;
            $maxPossible += $maxDim;
        }

        // 计算综合拖延指数
        $procrastinationIndex = round(($totalScore / $maxPossible) * 100, 1);
        
		//建议
		$suggestion = [];
		$suggestions = $this->generateAdvice($dimensionScores);
		foreach ($suggestions as $item) {
			$suggestion[] = $item;
		}
        return [
            'score' => $procrastinationIndex,
            'level' => $this->getRiskLevel($procrastinationIndex),
            'suggestion' => $suggestion,
            'dimensions' => $dimensionScores,
        ];
    }

    // 风险等级判定
    private function getRiskLevel($score) {
        if ($score >= 70) return '严重拖延';
        if ($score >= 50) return '中度拖延';
        if ($score >= 30) return '轻度拖延';
        return '无拖延倾向';
    }
	// 生成诊断建议
    public function generateAdvice($scores) {
        $advice = [];
        foreach ($scores as $dim => $score) {
            if ($score >= 60) {
                $advice[] = $this->getDimensionAdvice($dim);
            }
        }
        return array_unique($advice);
    }

    private function getDimensionAdvice($dim) {
        $adviceMap = [
            '任务启动' => '使用"2分钟法则"立即启动任务：任何能在2分钟内开始的事，现在就开始做',
            '时间感知' => '进行时间日志记录，每次任务前用三时估算法（最佳/正常/最差情况）预测时间',
            '分心抵抗' => '建立专注仪式：①关闭通知 ②使用Forest App ③设置物理隔离区',
            '自我调节' => '实施"番茄工作法+承诺机制"：完成4个番茄钟后奖励自己15分钟娱乐'
        ];
        return $adviceMap[$dim] ?? '加强该维度的行为训练';
    }
}

?>